﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;

namespace WinHeatTransfer
{
    public partial class FormSplav : Form
    {
        public FormSplav(IEnumerable<Material> materials)
        {
            InitializeComponent();

            _materials = materials;
        }

        #region === members ===

        IEnumerable<Material> _materials = null;

        const int _maxMaterial = 4;
        Control[] _labels = null;
        Control[] _text = null;
        Control[] _procs = null;

        #endregion

        public Material Splav { get; private set; }

        private void FormSplav_Load(object sender, EventArgs e)
        {
            _labels = new Control[] { lbl1, lbl2, lbl3, lbl4 };
            _text = new Control[] { txt1, txt2, txt3, txt4 };
            _procs = new Control[] { labelProc1, labelProc2, lblProc3, lblProc4 };

            for (int i = 0; i < _maxMaterial; i++)
            {
                _labels[i].Visible = false;
                _text[i].Visible = false;
                _procs[i].Visible = false;
            }

            listBox1.BeginUpdate(); 
            listBox1.Items.AddRange(_materials.ToArray());
            listBox1.EndUpdate();
        }

        private void btnTo_Click(object sender, EventArgs e)
        {
            if (listBox2.Items.Count == _maxMaterial)
                return;

            int selectedIndex = listBox1.SelectedIndex;

            if (selectedIndex < 0)
                return;

            Material material = listBox1.SelectedItem as Material;
            listBox2.Items.Add(material);
            listBox1.Items.Remove(material);

            int index = listBox2.Items.Count - 1;

            _labels[index].Visible = true;
            _text[index].Visible = true;
            _procs[index].Visible = true;

            _labels[index].Text = material.Name;
            _text[index].Text = "50";
        }

        private void btnFrom_Click(object sender, EventArgs e)
        {
            if (listBox2.Items.Count == 0)
                return;

            int selectedIndex = listBox2.SelectedIndex;

            if (selectedIndex < 0)
                return;

            Material material = listBox2.SelectedItem as Material;
            listBox1.Items.Add(material);
            listBox2.Items.Remove(material);

            for (int i = 0; i < _maxMaterial; i++)
            {
                _labels[i].Visible = false;
                _text[i].Visible = false;
                _procs[i].Visible = false;
            }

            for (int i = 0; i < listBox2.Items.Count; i++)
            {
                _labels[i].Visible = true;
                _text[i].Visible = true;
                _procs[i].Visible = true;

                _labels[i].Text = listBox2.Items[i].ToString();
            }
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            DialogResult = DialogResult.Cancel;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            int splavCount = listBox2.Items.Count;

            if (splavCount < 2)
            {
                string mes = "Сплав должен состоять как минимум из двух материалов.";
                MessageBox.Show(mes, "Ошибка в стартовых параметрах", MessageBoxButtons.OK, MessageBoxIcon.Stop);
                return;
            }

            int sum = 0;
            bool result = true;

            for (int i = 0; i < splavCount; i++)
            {
                if (!int.TryParse(_text[i].Text, out int n))
                {
                    result = false;
                    break;
                }

                sum += n;
            }

            if (!result || sum != 100)
            {
                string mes = "Процентное содержание указано некорректно.";
                MessageBox.Show(mes, "Ошибка в стартовых параметрах", MessageBoxButtons.OK, MessageBoxIcon.Stop);
                return;
            }

            string name = txtName.Text;

            if (name.Length < 1)
            {
                string mes = "Название сплава задано не корректно.";
                MessageBox.Show(mes, "Ошибка в стартовых параметрах", MessageBoxButtons.OK, MessageBoxIcon.Stop);
                return;
            }

            double conductivity = 0;
            double density = 0;
            double heatCapacity = 0;
            double meltingPoint = 0;

            for (int i = 0; i < splavCount; i++)
            {
                double percent = int.Parse(_text[i].Text);
                double w = percent / 100.0;
                Material material = listBox2.Items[i] as Material;

                conductivity += w * material.Conductivity;
                density += w * material.Density;
                heatCapacity += w * material.HeatCapacity;
                meltingPoint += w * material.MeltingPoint;
            }

            Splav = new Material
            { 
                Name = name,
                Conductivity = conductivity,
                Density = density,
                HeatCapacity = heatCapacity,
                MeltingPoint = meltingPoint
            };

            DialogResult = DialogResult.OK;
        }
    }
}
